package com.trussell.genesis.terrainmanipulation;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.light.DirectionalLight;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.MidPointHeightMap;
import com.jmex.terrain.util.ProceduralTextureGenerator;

public class Testy extends SimpleGame 
{
	TerrainBlock tb;
	
	public static void main(String[] args) {
		Testy t = new Testy();
		t.setConfigShowMode(ConfigShowMode.AlwaysShow);
		t.start();
	}
	
	@Override
	protected void simpleInitGame() {
		
		DirectionalLight dl = new DirectionalLight();
		dl.setEnabled(true);
		dl.setDirection(new Vector3f(-1,-1,0));
		lightState.attach(dl);
		
		// Generate a random terrain data
		MidPointHeightMap heightMap = new MidPointHeightMap(64, 1f);	// Scale the data
		Vector3f terrainScale = new Vector3f(4, .0575f, 4);
		// create a terrainblock
		tb = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale,
				heightMap.getHeightMap(), new Vector3f(0, 0, 0));

		tb.setModelBound(new BoundingBox());
		tb.updateModelBound();

		// generate a terrain texture with 3 textures
		ProceduralTextureGenerator pt = new ProceduralTextureGenerator(
				heightMap);
		
		ImageIcon ii1 = new ImageIcon("data/images/dirt.jpg");
		ImageIcon ii2 = new ImageIcon("data/images/rock.jpg");
		ImageIcon ii3 = new ImageIcon("data/images/ice.jpg");
		
		pt.addTexture(ii1, -128, 0, 128);
		pt.addTexture(ii2, 0, 128, 255);
		pt.addTexture(ii3, 128, 255,
				384);
		pt.createTexture(32);

		// assign the texture to the terrain
		TextureState ts = display.getRenderer().createTextureState();
		ts.setEnabled(true);
		Texture t1 = TextureManager.loadTexture(pt.getImageIcon().getImage(),
				Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);
		ts.setTexture(t1, 0);

		//load a detail texture and set the combine modes for the two terrain textures.
		Texture t2 = null;
		try {
			t2 = TextureManager.loadTexture(
					new File("data/images/dirt.jpg").toURI().toURL(),
							Texture.MinificationFilter.Trilinear,
							Texture.MagnificationFilter.Bilinear);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		ts.setTexture(t2, 1);
		t2.setWrap(Texture.WrapMode.Repeat);

		t1.setApply(Texture.ApplyMode.Combine);
		t1.setCombineFuncRGB(Texture.CombinerFunctionRGB.Modulate);
		t1.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);
		t1.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);
		t1.setCombineSrc1RGB(Texture.CombinerSource.PrimaryColor);
		t1.setCombineOp1RGB(Texture.CombinerOperandRGB.SourceColor);

		t2.setApply(Texture.ApplyMode.Combine);
		t2.setCombineFuncRGB(Texture.CombinerFunctionRGB.AddSigned);
		t2.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);
		t2.setCombineOp0RGB(Texture.CombinerOperandRGB.SourceColor);
		t2.setCombineSrc1RGB(Texture.CombinerSource.Previous);
		t2.setCombineOp1RGB(Texture.CombinerOperandRGB.SourceColor);

		tb.setRenderState(ts);
		//set the detail parameters.
		tb.setDetailTexture(1, 16);
		tb.setRenderQueueMode(Renderer.QUEUE_OPAQUE);

		tb.setRenderState(ts);
		
		rootNode.attachChild(tb);
	}
	
}
